package yy.zn.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import yy.zn.commom.Helper;
import yy.zn.entity.*;
import yy.zn.service.DataService;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author LiuLei
 */
@Api(tags = "服务器接口")
@RestController
@RequestMapping("/data")
public class DataController {

    @Resource
    private DataService dataService;

    @GetMapping("/moco")
    @ApiOperation("获取工单信息")
    @ApiImplicitParam(name = "mocCode", value = "任务令")
    public R<Moco> mocoList(String mocCode) throws Exception {
        if (Helper.isEmpty(mocCode))
            throw new Exception("产品编号不能为空！");
        return R.success(dataService.MOCO_LIST(mocCode));
    }

//    @GetMapping("/av")
//    @ApiOperation("根据到货单号获取到货单信息")
//    @ApiImplicitParam(name = "bar", value = "到货单条码")
//    public R<ArrivalDl> arrivalVouch(String bar) throws Exception {
//        if (Helper.isEmpty(bar))
//            throw new Exception("到货单编号不能为空！");
//        return R.success(dataService.ARRIVAL_VOUCH(bar));
//    }

    @GetMapping("/av")
    @ApiOperation("根据到货单号获取到货单信息")
    @ApiImplicitParam(name = "bar", value = "到货单条码")
    public R<ArrivalVouch> arrivalVouch(String bar) throws Exception {
        if (Helper.isEmpty(bar))
            throw new Exception("到货单编号不能为空！");
        return R.success(dataService.ARRIVAL_VOUCH(bar));
    }

    @GetMapping("/avl")
    @ApiOperation("根据单据获取到货单信息")
    @ApiImplicitParam(name = "code", value = "到货单单据")
    public R<List<ArrivalVouch>> arrivalVouchList(String code) throws Exception {
        if (Helper.isEmpty(code))
            throw new Exception("到货单单据不能为空！");
        return R.success(dataService.ARRIVAL_VOUCH_LIST(code));
    }

    @PostMapping("/ar")
    @ApiOperation("获取到货单详情信息")
    public R<List<ArrivalRepository>> arrivalRepository(@RequestBody ArrivalResDto dto) throws Exception {
        if (Helper.isEmpty(dto.getCode())) throw new Exception("到货单号不能为空！");
        if (Helper.isEmpty(dto.getInvCode())) throw new Exception("物料编号不能为空！");
        if (Helper.isEmpty(dto.getBatch())) throw new Exception("批号不能为空！");
        return R.success(dataService.ARRIVAL_REPOSITORY(dto));
    }

    @GetMapping("/gl")
    @ApiOperation("获取储位信息")
    @ApiImplicitParam(name = "bar", value = "储位条码")
    public R<Goods> goods(String bar) throws Exception {
        if (Helper.isEmpty(bar))
            throw new Exception("储位条码不能为空！");
        return R.success(dataService.GOODS(bar));
    }

    @GetMapping("/wh")
    @ApiOperation("获取仓库信息")
    public R<List<WareHouse>> warehouse() throws Exception {
        return R.success(dataService.WAREHOUSE());
    }

    @PostMapping("/csd")
    @ApiOperation("根据存户信息查询时间")
    @ApiImplicitParam(name = "bar", value = "储位条码")
    public R<String> currentStockDate(@RequestBody CurDate curDate) throws Exception {
        if (Helper.isEmpty(curDate.getWhCode())) throw new Exception("仓库编号不能为空！");
        if (Helper.isEmpty(curDate.getInvCode())) throw new Exception("存货编码不能为空！");
        // 取消批号判断
//        if (Helper.isEmpty(curDate.getBatch())) throw new Exception("批号不能为空！");
        return R.success(dataService.CURRENT_STOCK_DATE(curDate));
    }

    @PostMapping("/pos")
    @ApiOperation("获取存货信息")
    @ApiImplicitParam(name = "param", value = "[物料编号、仓库]")
    public R<List<Position>> position(@RequestBody Map<String, String> param) throws Exception {
        if (Helper.isEmpty(param.get("invCode")))
            throw new Exception("储位条码不能为空！");
        return R.success(dataService.POSITION(param));
    }

    @PostMapping("/inv")
    @ApiOperation("获取存货信息")
    @ApiImplicitParam(name = "param", value = "物料编号")
    public R<List<Position>> invStock(@RequestBody Map<String, String> param) throws Exception {
        if (Helper.isEmpty(param.get("invCode")))
            throw new Exception("储位条码不能为空！");
        return R.success(dataService.INV_STOCK(String.valueOf(param.get("invCode"))));
    }


    @PostMapping("/gls")
    @ApiOperation("批量获取储位信息")
    @ApiImplicitParam(name = "bar", value = "储位条码")
    public R<Map<String, Goods>> goodsLis(@RequestBody Set<String> bar) throws Exception {
        if (bar.isEmpty())
            throw new Exception("储位条码不能为空！");
        return R.success(dataService.GOODS_LIS(bar));
    }

    @GetMapping("/rs/{type}")
    @ApiOperation("根据类型获取收发类型")
    @ApiImplicitParam(name = "type", value = "收（1）/发（0）")
    public R<List<QueryInfo>> receiveOrSendType(@PathVariable Integer type) throws Exception {
        if (type == null) type = 0;
        return R.success(dataService.RECEIVE_OR_SEND_TYPE(type));
    }

    @GetMapping("/pt")
    @ApiOperation("获取采购类型")
    public R<List<QueryInfo>> purType() {
        return R.success(dataService.PUR_TYPE());
    }

    @GetMapping("/sal")
    @ApiOperation("获取销售类型")
    public R<List<QueryInfo>> saleType() {
        return R.success(dataService.SALE_TYPE());
    }

    @PostMapping("/slo")
    @ApiOperation("获取销售订单")
    @ApiImplicitParam(name = "order", value = "订单号")
    public R<Map<String, SaleOrder>> saleOrder(String order) {
        return R.success(dataService.SALE_ORDER(order));
    }

    @PostMapping("/aso")
    @ApiOperation("审核一张发货单")
    @ApiImplicitParam(name = "map", value = "审核条件")
    public R<Integer> auditSaleOrder(@RequestBody Map<String, Object> map) {
        return R.success(dataService.AUDIT_SALE_ORDER(map));
    }

    @GetMapping("/fccn")
    @ApiOperation("根据储位编号和名称查看库存是否存在")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "code", value = "储位编号"),
                    @ApiImplicitParam(name = "name", value = "储位名称")
            }
    )
    public R<Integer> findCurrentIsExistsByPositionCodeAndName(String code, String name) throws Exception {
        if (Helper.isEmpty(code)) {
            throw new Exception("储位编号不能为空！");
        }
        if (Helper.isEmpty(name)) {
            throw new Exception("储位名称不能为空！");
        }
        return R.success(dataService.findCurrentIsExistsByPositionCodeAndName(code, name));
    }

    @PostMapping("/pq")
    @ApiOperation("查询物料库存信息")
    @ApiImplicitParam(name = "bar", value = "物料条码")
    public R<List<Produce>> produceQty(String bar) {
        return R.success(dataService.PRODUCE_QTY(bar));
    }

    @PostMapping("/gbv")
    @ApiOperation("根据物料编号查询物料信息")
    @ApiImplicitParam(name = "bar", value = "物料条码")
    public R<List<InvCode>> getInvByInvCode(String bar) throws Exception {
        if (Helper.isEmpty(bar))
            throw new Exception("物料编号不能为空！");
        return R.success(dataService.GET_INV_BY_INVCODE(bar));
    }

    @PostMapping("/gbvs")
    @ApiOperation("根据物料编号查询物料信息")
    @ApiImplicitParam(name = "bar", value = "物料条码")
    public R<List<InvCode>> getInvByInvCodes(@RequestBody List<String> bar) throws Exception {
        if (bar.isEmpty())
            throw new Exception("物料编号不能为空！");
        Map<String, InvCode> invCodeMap = dataService.GET_INV_BY_INVCODE(bar).stream().collect(Collectors.toMap(InvCode::getInvCode,
                Function.identity()));
        return R.success(bar.stream().map(invCodeMap::get).collect(Collectors.toList()));
    }

    @PostMapping("/pi")
    @ApiOperation("查询领料单信息")
    @ApiImplicitParam(name = "bar", value = "生产订单条码")
    public R<List<ProduceInv>> produceIty(String bar) {
        return R.success(dataService.PRODUCE_INV(bar));
    }

    @PostMapping("/sbb")
    @ApiOperation("根据物料编号和批号倒货库存信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "invCode", value = "物料编号"),
            @ApiImplicitParam(name = "batch", value = "批号")
    })
    public R<InvStock> getStockByInvAndBatch(String invCode, String batch) {
        if (Helper.isEmpty(invCode)) throw new RuntimeException("物料编号不能为空！");
        if (Helper.isEmpty(batch)) throw new RuntimeException("批号不能为空！");
        InvStock invStock = dataService.GET_STOCK_BY_INV_AND_BATCH(invCode, batch);
        if (invStock == null) throw new RuntimeException("暂无物料库存信息");
        return R.success(invStock);
    }

    @PostMapping("/invs")
    @ApiOperation("现存量查询")
    @ApiImplicitParam(name = "body", value = "查询条件")
    public R<JsonPage<Inventory>> getInventory(@RequestBody InvQuery invQuery) {
        invQuery.build();
        return R.success(dataService.INVENTORY(invQuery));
    }

}
